import {
	getUserInfo, getUserItem
} from "../../api/user.js";

import {
	LOGIN_STATUS,
	UID,
	USER_INFO,
	PHONENUMBER
} from '@/config/cache';
import Cache from '@/utils/cache';

const state = {
	token: Cache.get(LOGIN_STATUS) || '',
	token_wx: Cache.get(LOGIN_STATUS) || '',
	backgroundColor: "#fff",
	userInfo: Cache.get(USER_INFO) ? JSON.parse(Cache.get(USER_INFO)) : null,
	uid: Cache.get(UID) || null,
	loginType: null,
	phoneNumber: Cache.get(PHONENUMBER) || null,
	tokenStr: uni.getStorageSync(`tokenStr_${process.env.ENV_TYPE}_3H5`) || "",
	isLockScroll: false,
};
const mutations = {
	LOGIN(state, opt) {
		// #ifdef H5 || APP-PLUS
		state.token = opt.token;
		Cache.set(LOGIN_STATUS, opt.token);
		// #endif
		
		// #ifdef MP
		state.token_wx = opt.token;
		Cache.set(LOGIN_STATUS, opt.token);
		// #endif
	},
	SETUID(state, val) {
		state.uid = val;
		Cache.set(UID, val);
	},
	UPDATE_LOGIN(state, token) {
		// #ifdef MP
		state.token_wx = token;
		// #endif
		// #ifdef H5
		state.token = token;
		// #endif

	},
	LOGOUT(state) {
		Cache.clear(LOGIN_STATUS);
		Cache.clear(UID);
		Cache.clear(USER_INFO);
		state.token = undefined;
		state.token_wx = undefined;
		state.uid = undefined
	},
	UPDATE_USERINFO(state, userInfo) {
		state.userInfo = userInfo;
		Cache.set(USER_INFO, userInfo);
	},
	SET_LOGINTYPE(state, loginType) {
		state.loginType = loginType
	},
	SET_PHONENUMBER(state, number) {
		state.phoneNumber = number
		Cache.set(PHONENUMBER, number);
	},
	SRTTOKENSTR(state, data) {
		state.tokenStr = data
		Cache.set(`tokenStr_${process.env.ENV_TYPE}_3H5`, data)
	},
	SET_SCROLL_LOCK(state, payload) {
		const isLock = !!payload
		state.isLockScroll = isLock

	}
};
const actions = {
	USERINFO({ state, commit }, force) {
		return new Promise(reslove => {
			getUserInfo().then(async rsp => {
				// let rsp = await getUserItem({ memberId: res.data.id })
				commit("SET_PHONENUMBER", rsp.data.phone)
				commit("UPDATE_USERINFO", rsp.data);
				reslove(rsp.data);
			});
		}).catch(() => {

		});

	}
};
export default {
	state,
	mutations,
	actions
};